#!/bin/bash

# 安装相同 仅配置资源部分不同

# 安装依赖环境
yum install -y gcc wget libnfnetlink-devel libnfnetlink ipvsadm libnl libnl-devel libnl3 libnl3-devel lm_sensors-libs \
  net-snmp-agent-libs net-snmp-libs  openssh-server openssh-clients  openssl openssl-devel automake iproute

# 解压编译安装
wget http://www.keepalived.org/software/keepalived-2.0.10.tar.gz
tar xvf keepalived-2.0.10.tar.gz -C /usr/local/src/
cd /usr/local/src/keepalived-2.0.10
./configure --prefix=/usr/local/keepalived --disable-fwmark
 make && make install

# 配置 keepalived
cp keepalived/etc/init.d/keepalived.rh.init /etc/sysconfig/keeplived.sysconfig
cp bin/keepalived /usr/sbin/
mkdir /etc/keepalived
echo "include /etc/keepalived/vip/*.conf" > /etc/keepalived/keepalived.conf


################################## 配置资源[ha_1] ##############################
vip_1=192.168.2.100
host_1_ip_0=192.168.2.71
host_1_ip_1=192.168.11.71

vip_2=192.168.11.100
host_2_ip_0=192.168.2.72
host_2_ip_1=192.168.11.72


mkdir -p /etc/keepalived/vip
echo 'include /etc/keepalived/vip/*.conf' >/etc/keepalived/keepalived.conf

# host_1
cat <<EOF    >/etc/keepalived/vip/public.conf
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 80
    priority 100
    advert_int 1
    unicast_src_ip $host_1_ip_0
    unicast_peer {
        $host_2_ip_0
    }

    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        $vip_1 dev eth0 label eth0:0
    }
}
EOF

cat <<EOF    >/etc/keepalived/vip/local.conf
vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 81
    priority 50
    advert_int 1
    unicast_src_ip $host_1_ip_1
    unicast_peer {
        $host_2_ip_1
    }

    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        $vip_2 dev eth1 label eth1:0
    }
}
EOF

# 启动服务
systemctl start keepalived
systemctl enable keepalived


################################## 配置资源[ha_2] ##############################
vip_1=192.168.2.100
host_1_ip_0=192.168.2.71
host_1_ip_1=192.168.11.71

vip_2=192.168.11.100
host_2_ip_0=192.168.2.72
host_2_ip_1=192.168.11.72

mkdir -p /etc/keepalived/vip
echo 'include /etc/keepalived/vip/*.conf' >/etc/keepalived/keepalived.conf

# host_2
cat <<EOF    >/etc/keepalived/vip/public.conf
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 80
    priority 50
    advert_int 1
    unicast_src_ip $host_2_ip_0
    unicast_peer {
        $host_1_ip_0
    }

    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        $vip_1 dev eth0 label eth0:0
    }
}
EOF

cat <<EOF    >/etc/keepalived/vip/local.conf
vrrp_instance VI_2 {
    state MASTER
    interface eth1
    virtual_router_id 81
    priority 100
    advert_int 1
    unicast_src_ip $host_2_ip_1
    unicast_peer {
        $host_1_ip_1
    }

    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        $vip_2 dev eth1 label eth1:0
    }
}
EOF

# 启动服务
systemctl start keepalived
systemctl enable keepalived